package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getBusinessReportData() {
        /*
        {
            reportDate:null,
            todayNewMember :0,
            totalMember :0,
            thisWeekNewMember :0,
            thisMonthNewMember :0,
            todayOrderNumber :0,
            todayVisitsNumber :0,
            thisWeekOrderNumber :0,
            thisWeekVisitsNumber :0,
            thisMonthOrderNumber :0,
            thisMonthVisitsNumber :0,
            hotSetmeal :[
                {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
                {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
            ]
        }
        */
        //获取当前时间返回
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String reportDate = sdf.format(new Date());
        //获取当前周的会员数量时，首先需要得到当前周的周一的日期
        //第一步，先获取当前是周几
        //第二步，往前推几天
        Date weekMonday = DateUtils.getThisWeekMonday(new Date());
        String monday = sdf.format(weekMonday);

        //获取当月1号的日期
        SimpleDateFormat sdf4month = new SimpleDateFormat("yyyy-MM");
        String firstDay = sdf4month.format(new Date())+"-1";

        //查询当天新增的会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(reportDate);
        //查询总会员数量
        Integer totalMember = memberDao.findMemberTotalCount();
        //查询本周新增会员数(即本周周一之后的会员数)
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(monday);
        //查询本月新增会员数(即本月1号之后的会员数)
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay);

        //查询当天预约数量
        Integer todayOrderNumber = orderDao.findOrderCountByDate(reportDate);
        //查询当天体检数量(预约后到达提交，visit=true)
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(reportDate);
        //查询本周预约数量
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(monday);
        //查询本周体检数量
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(monday);
        //查询本月预约数量
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay);
        //查询本月体检数量
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay);
        //map.put("reportDate","2020-03-28");
        //map.put("todayNewMember","1");
        //map.put("totalMember","2");
        //map.put("thisWeekNewMember","3");
        //map.put("thisMonthNewMember","4");
        //map.put("todayOrderNumber","5");
        //map.put("todayVisitsNumber","6");
        //map.put("thisWeekOrderNumber","7");
        //map.put("thisWeekVisitsNumber","8");
        //map.put("thisMonthOrderNumber","9");
        //map.put("thisMonthVisitsNumber","2020-03-28");

        //封装热门套餐(每个套餐只查询套餐名称，预约数量，预约占比)
        List<Map<String,Object>> hostSetmeal = orderDao.findHotSetmeal();

        Map<String,Object> map = new HashMap<>();
        map.put("reportDate",reportDate);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hostSetmeal);
        return map;
    }
}
